
import javax.microedition.rms.RecordComparator;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreNotOpenException;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author FFK
 */
public class rwBC {
    
    private RecordStore rs = null;
    private RecordStore rs_an = null;
    private RecordStore rs_score = null;
    public  static final String REC_STORE_SCORE = "score";
    public  static final String REC_STORE = "db";
    public static final String REC_STORE_AN = "dbAN";
    public int[]  array ;
    public int[]  array_an ;
    public int[]  array_score ;
    public int level =0;
    public int nMin = 0;
    public String score;
    public rwBC(){
        array = new int[81];
        array_an = new int[81];
        array_score = new int[10];
        for(int i = 0; i< 10; i++){
            array_score[i] = 0;
        }
        doc_sc();
    }
    public void doc_sc(){
        openRecStore();
        int len;
        try{
            byte[] recData = new byte[50];
               for(int i = 1; i <= rs_score.getNumRecords(); i++){
                   if(rs_score.getRecordSize(i) > recData.length)
                       recData = new byte[rs_score.getRecordSize(i)];
                   len = rs_score.getRecord(i, recData, 0);
                   score = new String(recData, 0, len);
                   array_score[i-1] = Integer.parseInt(score);
                   System.out.println(array_score[i-1]);
               }
        }catch(Exception ex){}

        
        closeStore();
        sortarray_score();
        for(int i =0; i< 10; i++){
            System.out.println(array_score[i]);
        }
    }
    public void write_sc(String s/*, String name*/) throws RecordStoreNotOpenException, RecordStoreException{
        int num = Integer.parseInt(s);
        System.out.println("gia tri ghi xuong:" +num);
        sortarray_score();
        if(num >= array_score[9]){
            array_score[9] = num ;
        }
         sortarray_score();
        for(int i = 0; i <= 10; i++){
            String tam = Integer.toString(array_score[i]);
            byte[] rec = tam.getBytes();
            rs_score.addRecord(rec, 0, rec.length);
            System.out.println("in:" +tam);
        }
        
    }
    public void doc() {
        openRecStore();
        try{
        readRecore();
        }catch(Exception e){}
        closeStore();
    }
    public void openRecStore() {
        try{
            rs = RecordStore.openRecordStore(REC_STORE, true);
            rs_an = RecordStore.openRecordStore(REC_STORE_AN, true);
            rs_score = RecordStore.openRecordStore(REC_STORE_SCORE, true);
        }
        catch(Exception e){
            
        }
    }

    public void writeRecore(String s, String s_an) throws RecordStoreNotOpenException, RecordStoreException {
        byte[] rec = s.getBytes();
        rs.addRecord(rec, 0, rec.length);
        System.out.println(s);
        
        byte[] rec_an = s_an.getBytes();
        rs_an.addRecord(rec_an, 0, rec_an.length);
        System.out.println("an ne :" +s_an);
    }
    public void sortarray_score(){
        int tam;
        String tam_user;
        for(int i = 0; i< 10; i++){
            for(int j = i+1; j< 10; j++)
                if(array_score[i] <= array_score[j]){// nho ve dau mang
                    tam = array_score[i];
                    array_score[i] = array_score[j];
                    array_score[j] = tam;
                }
            
        }
    }
    public  void readRecore()  {
        int len;
        try{
            byte[] recData = new byte[50];
               for(int i = 1; i <= rs.getNumRecords(); i++){
                   if(rs.getRecordSize(i) > recData.length)
                       recData = new byte[rs.getRecordSize(i)];
                   len = rs.getRecord(i, recData, 0);
                   String tam = new String(recData, 0, len);
                   array[i-1] = Integer.parseInt(tam);
                   System.out.println(array[i-1]);
               }
        }catch(Exception ex){}

        
        int len_an;
        try{
            byte[] recData_an = new byte[50];
               for(int i = 1; i <= rs_an.getNumRecords(); i++){
                   if(rs_an.getRecordSize(i) > recData_an.length)
                       recData_an = new byte[rs_an.getRecordSize(i)];
                   len_an = rs_an.getRecord(i, recData_an, 0);
                   String tam = new String(recData_an, 0, len_an);
                   array_an[i-1] = Integer.parseInt(tam);
                   System.out.println(array_an[i-1]);
               }
        }catch(Exception ex){}
        
        
    }
    public void closeStore(){
        try {
            rs.closeRecordStore();
//            rs_user.closeRecordStore();
            rs_an.closeRecordStore();
            rs_score.closeRecordStore();
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    public void deleteStore(){
        if(RecordStore.listRecordStores() != null){
            try {
                RecordStore.deleteRecordStore(REC_STORE);
                RecordStore.deleteRecordStore(REC_STORE_AN);
                RecordStore.deleteRecordStore(REC_STORE_SCORE);
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
   public boolean  check(){
           for(int j = 0; j< 81; j++){
               if(array[j]==1)
                   return true;
           }
           return false;
                   
   }
   public void level(){
       nMin = 0;
       for(int j = 0; j< 81; j++){
           if(array[j]==1) nMin++;
       }
       switch(nMin){
           case 10: level = 1;
               break;
           case 15: level = 2;
               break;
           case 20: level = 3;
               break;
           case 30: level = 4;
               break;
           case 40: level = 5;
               break;
       }
   }
    
    
}


class Comparator implements RecordComparator
{
  public int compare(byte[] rec1, byte[] rec2)
  {
    String str1 = new String(rec1), str2 = new String(rec2);
    
    int result = str1.compareTo(str2);
    if (result == 0)
      return RecordComparator.EQUIVALENT;
    else if (result < 0)
      return RecordComparator.PRECEDES;
    else
      return RecordComparator.FOLLOWS;          
  }
}
